//
//  AppDelegate.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/7/21.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabBarController.h"
#import "LoginVC.h"



#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    //
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
    
    //状态栏白色字体
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    UINavigationBar *bar = [UINavigationBar appearance];
    CGFloat rgb = 0.1;
    bar.tintColor = [UIColor whiteColor];
    bar.barTintColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.9];
    bar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    NSString *str = [NSString readToNSDefaultsWithKey:kIsLogined];
    if ([str isEqual:@"1"]) {
        self.window.rootViewController = [[MainTabBarController alloc] init];
    }else{
        self.window.rootViewController = [[LoginVC alloc] init];
    }
    
    
//    self.window.rootViewController = [[MainTabBarController alloc] init];
    
    
    //初始化应用，appKey和appSecret从后台申请得
    [SMSSDK registerApp:@"18858b25cc08c"
             withSecret:@"b99cf97097e398a3198ec8fafa5d942e"];
    

    /**
     *  设置ShareSDK的appKey，如果尚未在ShareSDK官网注册过App，请移步到http://mob.com/login 登录后台进行应用注册，
     *  在将生成的AppKey传入到此方法中。
     *  方法中的第二个第三个参数为需要连接社交平台SDK时触发，
     *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发，根据返回的平台类型来配置平台信息。
     *  如果您使用的时服务端托管平台信息时，第二、四项参数可以传入nil，第三项参数则根据服务端托管平台来决定要连接的社交SDK。
     */
    [ShareSDK registerApp:@"1771154779371"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
                                       appSecret:@"64020361b8ec4c99936c0e3999a9f249"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"100371282"
                                      appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                    authType:SSDKAuthTypeBoth];
                 break;
            default:
                 break;
         }
     }];
    return YES;
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    //进入后台
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    //进入前台
    //保持登陆状态
    NSString *loginStr = [NSString readToNSDefaultsWithKey:kIsLogined];
    if ([loginStr isEqual:@"1"]) {
        //参数
        NSDictionary *parameters = @{
                                     @"phone":[NSString readToNSDefaultsWithKey:kPhone],
                                     @"pass": [NSString readToNSDefaultsWithKey:kPass],
                                     };
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager POST:@"http://115.28.152.130/tour/v1/login.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"登陆失败"];
        }];
    }
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // 进程被杀掉
    //清除缓存user leader
    [@"" saveToNSDefaultsWithKey:kPhotoUrl];
    [@"" saveToNSDefaultsWithKey:kRealName];
    [@"" saveToNSDefaultsWithKey:kNickName];
    [@"" saveToNSDefaultsWithKey:kSex];
    [@"" saveToNSDefaultsWithKey:kCity];
    [@"" saveToNSDefaultsWithKey:kPhone];
    [@"" saveToNSDefaultsWithKey:kPass];
    [@"" saveToNSDefaultsWithKey:kAge];
    [@"" saveToNSDefaultsWithKey:kCard];
    [@"" saveToNSDefaultsWithKey:kIsLogined];
    [@"" saveToNSDefaultsWithKey:kType];
    
    //清除缓存club
    [@"" saveToNSDefaultsWithKey:kClubPhotoUrl];
    [@"" saveToNSDefaultsWithKey:kEmail];
    //club
    [@"" saveToNSDefaultsWithKey:kIdCardFimgUrl];
    [@"" saveToNSDefaultsWithKey:kIdCardBimgUrl];
    [@"" saveToNSDefaultsWithKey:kStatus];
    [@"" saveToNSDefaultsWithKey:kClubName];
    [@"" saveToNSDefaultsWithKey:kClubSummary];
}

@end
